package cn.lg.soar.boot.fc.controller;

import cn.lg.soar.boot.fc.dao.ExtendFieldDao;
import cn.lg.soar.boot.fc.dao.FcFormCollectDetailDao;
import cn.lg.soar.boot.fc.entity.FormItem;
import cn.lg.soar.boot.fc.entity.FormResult;
import cn.lg.soar.boot.fc.pojo.ExtendField;
import cn.lg.soar.boot.fc.service.FormItemService;
import cn.lg.soar.boot.fc.service.FormResultService;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.group.Create;
import cn.lg.soar.core.annotations.constraints.group.Update;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.mvc.enums.ControllerConst;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.mvc.annotations.Log;
import cn.lg.soar.system.api.manager.AccountManager;
import cn.lg.soar.system.api.model.AuthUser;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.lg.soar.boot.fc.entity.FormCollect;
import cn.lg.soar.boot.fc.service.FormCollectService;

/**
 * 表单收集
 * @author ZhuanZ（无密码）
 * @since 2025-01-09
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Api(tags = "表单收集")
@Validated
@RestController
@RequestMapping("fc/formCollect")
public class FormCollectController extends BaseController<FormCollect, FormCollectService> {
    @Resource
    private ExtendFieldDao extendFieldDao;
    @Resource
    private FcFormCollectDetailDao detailDao;
    @Resource
    private FormResultService formResultService;
    @Resource
    private FormItemService formItemService;
    private SqueryWapper<FormItem> squeryWapper;

    @Override
    protected Serializable getId(FormCollect entity) {
        return entity.getId();
    }

    @Log(value = "新增表单收集", data = "#p0", dataId = "#$r.data")
    @IdempotentByParam("#p0")
    @ApiOperation(value = ControllerConst.CREATE_NAME)
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody @Validated(Create.class) FormCollect data) {
        return super.createResult(data);
    }

    @Log(value = "修改表单收集", data = "#p0", dataId = "#p0.id")
    @ApiOperation(value = ControllerConst.UPDATE_NAME)
    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Void> update(@RequestBody @Validated(Update.class) FormCollect data) {
        return super.updateResult(data);
    }

    @Log(value = "删除表单收集", data = "#p0")
    @ApiOperation(value = ControllerConst.REMOVE_NAME, notes = "根据 id 删除")
    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @ApiOperation(value = ControllerConst.DETAIL_NAME, notes = "根据 ID 获取一条数据")
    @RequestMapping(path = "detail", method = RequestMethod.GET)
    public HttpResult<FormCollect> detail(@RequestParam Long id) {
        return super.detailResult(id);
    }

    @ApiOperation(value = ControllerConst.QUERY_NAME, notes = "多功能查询（分页、列表、统计）")
    @RequestMapping(path = "query", method = RequestMethod.GET)
    public HttpResult<PageResult<FormCollect>> query(PageQuery pageQuery, QueryParams params){
        return super.queryResult(pageQuery, params);
    }

    @Log(value = "导出表单收集")
    @ApiOperation(value = ControllerConst.EXPORT_NAME)
    @RequestMapping(path = "export", method = RequestMethod.GET)
    public void exports(String filename, QueryParams params, HttpServletResponse resp){
        super.exportExcel(filename, params, resp);
    }

    @Log(value = "导入表单收集")
    @ApiOperation(value = ControllerConst.IMPORT_NAME)
    @RequestMapping(path = "import", method = RequestMethod.POST)
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @Log(value = "查询自定义字段")
    @ApiOperation(value = "查询自定义字段", notes = "查询自定义字段")
    @RequestMapping(path = "getExtendList", method = RequestMethod.GET)
    public HttpResult<List<ExtendField>> getExtendList(@RequestParam Long collectId) {
        ExtendField extendField = new ExtendField();
        extendField.setExtendType("bd"+collectId);
        List<ExtendField> list = extendFieldDao.findList(extendField);
        return HttpResult.data(list);
    }

    @Log(value = "添加自定义字段")
    @ApiOperation(value = "添加自定义字段")
    @RequestMapping(path = "addExtend", method = RequestMethod.POST)
    public HttpResult<Void> addExtend(@RequestBody @Valid ExtendField extendField) {
//        extendField.setExtendType("cj");
        extendFieldDao.save(extendField);
        return HttpResult.success("添加自定义字段成功");
    }

    @Log(value = "更新自定义字段")
    @ApiOperation(value = "更新自定义字段")
    @RequestMapping(path = "updateExtend", method = RequestMethod.POST)
    public HttpResult<Void> updateExtend(@RequestBody @Valid ExtendField extendField) {
        extendFieldDao.save(extendField);
        return HttpResult.success("更新自定义字段成功");
    }
}